import CommonEvent from "../common/CommonEvent";
import EventNotice from "../common/tool/EventNotice";
import IRegisterEvent from "../expand/interface/IRegisterEvent";
import LayerBase from "../expand/ui/LayerBase";
import ContextPool from "../common/tool/context/ContextPool";
import LockStatus from "../expand/base/LockStatus";
import GameUi from "./GameUi";
import { IGameState } from "./IGameState";
import List from "../common/tool/List";
import LogTool from "../common/tool/LogTool";
import GameEvent from "./config/GameEvent";
import GameSnake from "./GameSnake";
import GameMap from "./GameMap";
import CameraContol from "./CameraContol";
import NativeMgr from "../../../general/Script/mgr/nativeManager";

export enum GameState {
    /**
     *   开始
     */
    Start,
    /**
     *   暂停
     */
    Pause,
    /**
     *   胜利
     */
    Win,
    /**
     *   结束
     */
    Over,
}

const { ccclass, property } = cc._decorator;
@ccclass
export default class GameWorldSnake extends LayerBase implements IGameState, IRegisterEvent {
    /**
 *  游戏世界状态锁标签
 */
    public static readonly GAME_WORLD_STATUS = "GAME_WORLD_STATUS";

    @property(cc.Node)
    public NodeTouch: cc.Node = null;

    public CameraContol: CameraContol = null;

    public GameSnake: GameSnake = null;

    hitTimes: number = 0;

    @property(GameMap)
    public GameMap: GameMap = null;

    private mContextPool: ContextPool = null;
    private mLockStatus: LockStatus = null;
    private mIsTouch: boolean = false;
    public get IsTouch(): boolean {
        return this.mIsTouch;
    }
    private mGameUi: GameUi = null;
    public get GameUi(): GameUi {
        return this.mGameUi;
    }

    /**
   *   状态池
   */
    private mGameStateList: any[] = []
    public get GameStateList(): any[] {
        return this.mGameStateList;
    }
    public isGameState(state: GameState): boolean {
        return this.mGameStateList.indexOf(state) != -1;
    }
    public addGameState(...states: GameState[]) {

        for (let state of states) {
            if (!this.isGameState(state)) {
                this.mGameStateList.push(state);
            }
        }
        return true;
    }
    public removeGameState(...states: GameState[]) {
        console.log('-----------status1111 = ', this.mGameStateList);
        for (let i = states.length - 1; i >= 0; i--) {
            console.log('states[i] = ', states[i]);
            for (let j = this.mGameStateList.length - 1; j >= 0; j--) {
                console.log('this.mGameStateList[i] = ', this.mGameStateList[j]);
                if (this.mGameStateList[j] == states[i]) {
                    this.mGameStateList.splice(j, 1);
                    break;
                }
            }

        }
        console.log('-----------status = ', this.mGameStateList);
    }

    protected start(): void {
        this.GameSnake = this.node.getChildByName("GameSnake").getComponent(GameSnake);
        this.CameraContol = this.node.getChildByName("CameraContol").getComponent(CameraContol);

        cc.systemEvent.on('snake-block-die', () => {
            this.hitTimes++;
            if (this.hitTimes >= 6) {
                this.hitTimes = 0;
                this.onGamePause();
                // NativeMgr.Ins().showExpressAds();
                cc.systemEvent.emit('showExpressAdInGame', 2);
            }


        }, this);

        cc.systemEvent.on("getEcpm", () => {
            this.onGameResume();
        }, this);

        this.init();
    }

    public init(): boolean {
        this.mContextPool = new ContextPool(this.name);
        this.mContextPool.insert(this);

        this.mGameUi = this.node.getComponent(GameUi);
        this.mGameUi.init(this.mContextPool);
        this.mContextPool.insert(this.mGameUi);

        this.mLockStatus = new LockStatus(GameWorldSnake.GAME_WORLD_STATUS);

        this.GameSnake.init(this.mContextPool);
        this.mContextPool.insert(this.GameSnake);

        this.GameMap.init(this.mContextPool);
        this.mContextPool.insert(this.GameMap);

        this.CameraContol.init(this.mContextPool);
        this.mContextPool.insert(this.CameraContol);

        this.scheduleOnce(this.onContextLoad, 0.1);
        this.schedule(this.updateWorld, 0.1);

        this.onRegisterEvent();
        return true;
    }

    public onContextLoad() {
        this.mContextPool.load();
        this.onGameStart();
    }

    protected onDestroy(): void {
        this.onCancelEvent();
    }

    public onRegisterEvent(): void {
        cc.macro.ENABLE_MULTI_TOUCH = false;
        this.NodeTouch.on(cc.Node.EventType.TOUCH_START, (event) => {
            if (this.mLockStatus.isBusy() || this.isGameBusy())
                return;
            this.mIsTouch = true;
            this.GameSnake.onTouchBegin(event);
        });

        this.NodeTouch.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
            if (this.mLockStatus.isBusy() || this.isGameBusy())
                return;
            this.mIsTouch = true;
            this.GameSnake.onTouchMove(event);
        });

        this.NodeTouch.on(cc.Node.EventType.TOUCH_END, (event) => {
            if (this.mLockStatus.isBusy() || this.isGameBusy())
                return;
            this.mIsTouch = false;
            this.GameSnake.onTouchEnd(event);
        });

        this.NodeTouch.on(cc.Node.EventType.TOUCH_CANCEL, (event) => {
            if (this.mLockStatus.isBusy() || this.isGameBusy())
                return;;
            this.mIsTouch = false;
            this.GameSnake.onTouchCancel(event);
        });

        EventNotice.on(CommonEvent.COMMON_EVENT_GAME_WIN, this.onGameWin, this);
        EventNotice.on(CommonEvent.COMMON_EVENT_GAME_OVER, this.onGameOver, this);
        EventNotice.on(CommonEvent.COMMON_EVENT_GAME_PAUSE, this.onGamePause, this);
        EventNotice.on(CommonEvent.COMMON_EVENT_GAME_RESUME, this.onGameResume, this);
        EventNotice.on(CommonEvent.COMMON_EVENT_SAVE_GAME_DATA, this.onSaveGameData, this);
    }

    public onCancelEvent(): void {
        EventNotice.off(CommonEvent.COMMON_EVENT_GAME_WIN, this);
        EventNotice.off(CommonEvent.COMMON_EVENT_GAME_OVER, this);
        EventNotice.off(CommonEvent.COMMON_EVENT_GAME_PAUSE, this);
        EventNotice.off(CommonEvent.COMMON_EVENT_GAME_RESUME, this);
        EventNotice.off(CommonEvent.COMMON_EVENT_SAVE_GAME_DATA, this);

        this.mLockStatus.destroy();
        this.mContextPool.destroy();
    }

    public updateWorld(dt): void {
        if (this.isGameBusy())
            return;
    }

    public update(dt): void {
        if (this.isGameBusy()) {
            this.GameSnake.resetY();
            return;
        }

        this.GameSnake.onUpdate(dt);
        this.GameMap.onUpdate(dt);
        this.CameraContol.onUpdate(dt);
    }

    public isBusy(): boolean {
        if (!this.isGameState(GameState.Start))
            return true;
        if (this.isGameState(GameState.Over))
            return true;
        if (this.isGameState(GameState.Pause))
            return true;
        if (this.isGameState(GameState.Win))
            return true;



        return false;
    }

    public isGameBusy(): boolean {
        if (this.isBusy())
            return true;
        if (this.mLockStatus.isBusy())
            return true;
        return false;
    }


    public onGameStart(): void {
        this.addGameState(GameState.Start);
        this.removeGameState(GameState.Pause, GameState.Win, GameState.Over);
        this.GameUi.onGameStart();
    }

    public onGameWin(): void {
        if (this.isGameState(GameState.Win)) {
            LogTool.LogWarning("重复胜利!!!");
            return;
        }
        this.addGameState(GameState.Win);
        this.GameUi.onGameWin();
    }

    public onGameOver(): void {
        if (this.isGameState(GameState.Over)) {
            LogTool.LogWarning("重复失败!!!");
            return;
        }
        this.CameraContol.node.active = false;
        this.addGameState(GameState.Over);
        this.GameUi.onGameOver();
    }

    public onGamePause(): void {
        if (this.isGameState(GameState.Pause))
            return;
        this.addGameState(GameState.Pause);
        this.GameUi.onGamePause();
    }

    public onGameResume(): void {
        console.log('3333333333333333333');
        if (!this.isGameState(GameState.Pause))
            return;
        console.log('4444444444444444444444444');
        this.removeGameState(GameState.Pause);
        console.log('5555555555555555555555555555');
        this.GameUi.onGameResume();
    }

    public onGameRestart(): void {
        this.removeGameState(GameState.Pause, GameState.Win, GameState.Over);
        this.addGameState(GameState.Start);
        this.GameUi.onGameRestart();
    }

    public onGameRevive(): void {
        this.removeGameState(GameState.Over);
        this.GameUi.onGameRevive();
    }


    /**
     * 保存游戏数据
     */
    public onSaveGameData() {
    }

    /**
   *  游戏世界进程锁增加
   */
    public static IncreaseLock(): void {
        LockStatus.Increase(GameWorldSnake.GAME_WORLD_STATUS);
    }

    /**
     *  游戏世界进程锁减少
     */
    public static ReduceLock(): void {
        LockStatus.Reduce(GameWorldSnake.GAME_WORLD_STATUS);
    }

    /**
     *  游戏世界进程锁减少
     */
    public static ResertLock(): void {
        LockStatus.Resert(GameWorldSnake.GAME_WORLD_STATUS);
    }

    public static GameOver(): void {
        EventNotice.emit(CommonEvent.COMMON_EVENT_GAME_OVER);
    }
}
